<?php
// +----------------------------------------------------------------------
// | HkCms 后台用户权限模型
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\admin\model;

use common\model\Model;

class Access extends Model
{
    /**
     * 根据角色ID返回全部权限
     * @param integer $roleid 角色ID
     * @return array|bool
     */
    public function getAccessList($roleid)
    {
        if (empty($roleid)) {
            return false;
        }
        //子角色列表
        $child = explode(',', model("admin/Role")->getArrchildid($roleid));
        if (empty($child)) {
            return false;
        }
        //查询出该角色拥有的全部权限列表
        $data = $this->where(array('role_id' => array('IN', $child)))->select()->toArray();
        if (empty($data)) {
            return false;
        }
        $accessList = array();
        foreach ($data as $info) {
            unset($info['status']);
            $accessList[] = $info;
        }
        return $accessList;
    }

    /**
     * 检查用户是否有对应权限
     * @param string $map 方法[模块/控制器/方法]，为空自动获取
     * @return bool
     */
    public function isCompetence($map = '')
    {
        $module_name = request()->module();
        $controller_name = request()->controller();
        $action_name =  request()->action();
        //超级管理员
        if (\app\admin\service\User::getInstance()->isAdministrator()) {
            return true;
        }
        if (!is_array($map)) {
            //子角色列表
            $child = explode(',', model("admin/Role")->getArrchildid(\app\admin\service\User::getInstance()->role_id));
            if (!empty($map)) {
                $map = trim($map, '/');
                $map = explode('/', $map);
                if (empty($map)) {
                    return false;
                }
            } else {
                $map = array($module_name, $controller_name, $action_name,);
            }
            if (count($map) >= 3) {
                list($app, $controller, $action) = $map;
            } elseif (count($map) == 1) {
                $app = $module_name;
                $controller = $controller_name;
                $action = $map[0];
            } elseif (count($map) == 2) {
                $app = $module_name;
                list($controller, $action) = $map;
            }
            $map = array('role_id' => array('IN', $child), 'app' => $app, 'controller' => $controller, 'action' => $action);
        }
        $count = $this->where($map)->count();
        return $count ? true : false;
    }

    /**
     * 角色授权
     * @param type $addauthorize 授权数据
     * @param type $roleid 角色id
     * @return boolean
     */
    public function batchAuthorize($addauthorize, $roleid = 0) 
    {
        if (empty($addauthorize)) {

            $this->error = '没有需要授权的权限！';

            return false;
        }

        if (empty($roleid)) {

            if (empty($addauthorize[0]['role_id'])) {

                $this->error = '角色ID不能为空！';

                return false;
            }

            $roleid = $addauthorize[0]['role_id'];
        }

        Cache('TOKEN_ON', false);

        foreach ($addauthorize as &$v) {
            
            unset($v['type']);
        }
        //删除旧的权限
        $this->where(array("role_id" => $roleid))->delete();

        return $this->saveAll($addauthorize) !== false ? true : false;
    }
}